package gui.curso;

import entidad.Aula;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.SystemColor;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import manejador.ManejadorDeAula;
import modelo.ModeloAula;

public class DialogoAula extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel panelProfesoreActivos = null;
	private JScrollPane jspProfesores = null;
	private JTable tblProfesores = null;
	private JPanel panelBuscar = null;
	private JLabel lblBuscar = null;
	private JComboBox cmbBuscar = null;
	private JTextField txtBuscar = null;
	private JButton btnAceptar = null;
	private JButton btnCancelar = null;
	private ModeloAula modeloAula = null;
	private static DialogoAula instancia = null;
	private static String accion = null;

	public static DialogoAula getInstancia() {
		if (instancia == null) {
			instancia = new DialogoAula(null, "Lista de Ciclos");
		}
		return instancia;
	}

	/**
	 * @param owner
	 */
	private DialogoAula(Frame owner, String titulo) {
		super(owner);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.modeloAula = ModeloAula.getInstancia();
		this.setSize(901, 410);
		this.setModal(true);
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo(null);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getPanelProfesoreActivos(), null);
			jContentPane.add(getPanelBuscar(), null);
			jContentPane.add(getBtnAceptar(), null);
			jContentPane.add(getBtnCancelar(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes panelProfesoreActivos
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelProfesoreActivos() {
		if (panelProfesoreActivos == null) {
			panelProfesoreActivos = new JPanel();
			panelProfesoreActivos.setLayout(null);
			panelProfesoreActivos.setBounds(new Rectangle(6, 90, 871, 227));
			panelProfesoreActivos.setBorder(BorderFactory.createTitledBorder(
					BorderFactory.createLineBorder(SystemColor.controlText, 1),
					"Lista de Ciclos", TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("Dialog",
							Font.BOLD, 14), SystemColor.controlText));
			panelProfesoreActivos.add(getJspProfesores(), null);
		}
		return panelProfesoreActivos;
	}

	/**
	 * This method initializes jspProfesores
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJspProfesores() {
		if (jspProfesores == null) {
			jspProfesores = new JScrollPane();
			jspProfesores.setBounds(new Rectangle(12, 25, 850, 185));
			jspProfesores.setViewportView(getTblProfesores());
		}
		return jspProfesores;
	}

	/**
	 * This method initializes tblProfesores
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getTblProfesores() {
		if (tblProfesores == null) {
			tblProfesores = new JTable(modeloAula);
		}
		return tblProfesores;
	}

	/**
	 * This method initializes panelBuscar
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelBuscar() {
		if (panelBuscar == null) {
			lblBuscar = new JLabel();
			lblBuscar.setBounds(new Rectangle(10, 21, 55, 23));
			lblBuscar.setText("Buscar:");
			panelBuscar = new JPanel();
			panelBuscar.setLayout(null);
			panelBuscar.setBounds(new Rectangle(255, 18, 445, 58));
			panelBuscar.setBorder(BorderFactory.createTitledBorder(
					BorderFactory.createLineBorder(Color.black, 1), "Busqueda",
					TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("SansSerif",
							Font.PLAIN, 12), Color.black));
			panelBuscar.add(lblBuscar, null);
			panelBuscar.add(getCmbBuscar(), null);
			panelBuscar.add(getTxtBuscar(), null);
		}
		return panelBuscar;
	}

	/**
	 * This method initializes cmbBuscar
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getCmbBuscar() {
		if (cmbBuscar == null) {
			cmbBuscar = new JComboBox();
			cmbBuscar.setBounds(new Rectangle(67, 21, 151, 23));
			cmbBuscar.setModel(new DefaultComboBoxModel(new String[] {
					"ID Aula","Nombre" ,"Descripcion"}));
		}
		return cmbBuscar;
	}

	/**
	 * This method initializes txtBuscar
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtBuscar() {
		if (txtBuscar == null) {
			txtBuscar = new JTextField();
			txtBuscar.setBounds(new Rectangle(221, 21, 210, 23));
		}
		return txtBuscar;
	}

	/**
	 * This method initializes btnAceptar
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnAceptar() {
		if (btnAceptar == null) {
			btnAceptar = new JButton();
			btnAceptar.setBounds(new Rectangle(342, 332, 106, 27));
			btnAceptar.setText("Aceptar");
			btnAceptar.setIcon(new ImageIcon("./lib/images/add.png"));
			btnAceptar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (tblProfesores.isRowSelected(tblProfesores
							.getSelectedRow()) == false) {
						JOptionPane.showMessageDialog(null,
								"Debe de seleccionar un ciclo de la lista",
								"Error", JOptionPane.ERROR_MESSAGE);
					} else {

						int opcion = JOptionPane
								.showConfirmDialog(
										null,
										"Esta seguro que desea asignar este ciclo al curso ? ",
										"Confirmando",
										JOptionPane.YES_NO_OPTION);

						if (opcion == JOptionPane.YES_OPTION) {

							int id = Integer.parseInt(tblProfesores.getValueAt(
									tblProfesores.getSelectedRow(), 0)
									.toString());
							Aula aula = (Aula) ManejadorDeAula.getInstancia()
									.getEntidadPorId(id);

							if (accion.equalsIgnoreCase("Agregar")) {

								PanelAgregarCurso.txtAula.setText(aula
										.getDescripcion());
								PanelAgregarCurso.setIdAula(id);

							} else if (accion.equalsIgnoreCase("Modificar")) {

								PanelModificarCurso.txtAula.setText(aula
										.getDescripcion());
								PanelModificarCurso.setIdAula(id);
							}
							DialogoAula.getInstancia().setVisible(false);
						}

					}
				}
			});
		}
		return btnAceptar;
	}

	/**
	 * This method initializes btnCancelar
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnCancelar() {
		if (btnCancelar == null) {
			btnCancelar = new JButton();
			btnCancelar.setBounds(new Rectangle(457, 332, 106, 27));
			btnCancelar.setText("Cancelar");
			btnCancelar.setIcon(new ImageIcon("./lib/images/salir.png"));
			btnCancelar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					DialogoAula.getInstancia().setVisible(false);
				}
			});
		}
		return btnCancelar;
	}

	public static String getAccion() {
		return accion;
	}

	public static void setAccion(String accion) {
		DialogoAula.accion = accion;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
